package com.cabrida.livestar.main;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.main.me.AttentionGvActivity;
import com.cabrida.livestar.main.me.GiftLvActivity;
import com.cabrida.livestar.main.me.InformationActivity;
import com.cabrida.livestar.main.me.LvActivity;
import com.cabrida.livestar.main.me.RedPacketLvActivity;
import com.cabrida.livestar.pay.PayActivity;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;

/**
 * 粉丝端我的
 * 
 * @author why
 */
public class FansMineView extends LinearLayout implements OnClickListener {
	private Context mContext;
	//我的红包
	private LinearLayout ll_packet;
	//我的礼品
	private LinearLayout ll_gift;
	//我关注的明星
	private LinearLayout ll_attention;
	//我的等级
	private LinearLayout ll_lv;
	
	private Button btn_buy;
	
	private Button btn_quit;
	
	private ImageView iv_back_bg;
	private TextView tv_title;
	private CircleImageView iv_header;
	private TextView tv_name;
	private TextView tv_account_num;
	private RelativeLayout rl_information;

	public FansMineView(Context context) {
		super(context);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_mine, this);
		Init(view);
	}

	/**
	 * 初始化控件
	 */
	private void Init(View view) {
		iv_back_bg = (ImageView) findViewById(R.id.iv_back_bg);
		tv_title = (TextView) findViewById(R.id.tv_title);
		rl_information = (RelativeLayout) findViewById(R.id.rl_information);
		rl_information.setOnClickListener(this);
		iv_header = (CircleImageView) findViewById(R.id.iv_header);
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_account_num = (TextView) findViewById(R.id.tv_account_num);
		ll_packet = (LinearLayout) findViewById(R.id.ll_packet);
		ll_packet.setOnClickListener(this);
		ll_gift = (LinearLayout) findViewById(R.id.ll_gift);
		ll_gift.setOnClickListener(this);
		ll_attention = (LinearLayout) findViewById(R.id.ll_attention);
		ll_attention.setOnClickListener(this);
		ll_lv = (LinearLayout) findViewById(R.id.ll_lv);
		ll_lv.setOnClickListener(this);
		btn_buy = (Button) findViewById(R.id.btn_buy);
		btn_buy.setOnClickListener(this);
		btn_quit = (Button) findViewById(R.id.btn_quit);
		btn_quit.setOnClickListener(this);
		
		setData();
	}
	
	private void setData(){
		iv_back_bg.setVisibility(View.INVISIBLE);
		tv_title.setText(mContext.getString(R.string.text_mine));
		MyUtils.loadImg(Global.headPic, iv_header);
		tv_name.setText(Global.U_nickname);
		tv_account_num.setText(Global.U_balance);
	}
	
	public void refreshPersonalData(){
		tv_name.setText(Global.U_nickname);
		MyUtils.loadImg(Global.headPic, iv_header);
	}
	
	
	private Dialog dialogQuit;
	
	public void popWindows() {
		showPayDialog();
	}

	private void showPayDialog() {
		initPayDialogView();
		if (null != dialogQuit && dialogQuit.isShowing()) {
			return;
		}
		if (null != dialogQuit) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogQuit.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) mContext).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogQuit.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogQuit.setCanceledOnTouchOutside(true);
			dialogQuit.show();
		}
	}

	private void initPayDialogView() {
		LayoutInflater mInflater = LayoutInflater.from(mContext);
		View view = mInflater.inflate(R.layout.dialog_quit, null);

		Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialogQuit) {
					dialogQuit.dismiss();
				}
			}
		});
		
		Button btn_sure = (Button) view.findViewById(R.id.btn_sure);
		btn_sure.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialogQuit) {
					dialogQuit.dismiss();
					Intent intent = new Intent();
					intent.setAction(MainActivity.ACTION_QUIT);
					mContext.sendBroadcast(intent);
				}
			}
		});

		dialogQuit = new Dialog(mContext, R.style.dialogWindowStyle);
		dialogQuit.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogQuit.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}
	
	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.rl_information:
			intent = new Intent();
			intent.setClass(mContext, InformationActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_packet:
			intent = new Intent();
			intent.setClass(mContext, RedPacketLvActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_gift:
			intent = new Intent();
			intent.setClass(mContext, GiftLvActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_attention:
			intent = new Intent();
			intent.setClass(mContext, AttentionGvActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_lv:
			intent = new Intent();
			intent.setClass(mContext, LvActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.btn_buy:
			intent = new Intent();
			intent.setClass(mContext, PayActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.btn_quit:
			popWindows();
			break;
		default:
			break;
		}
	}

}
